import { assign } from 'min-dash';

export default function PaletteProvider(
  palette,
  create,
  elementFactory,
  spaceTool,
  lassoTool,
  handTool,
  globalConnect,
  translate,
) {
  this.create = create;
  this.elementFactory = elementFactory;
  this.spaceTool = spaceTool;
  this.lassoTool = lassoTool;
  this.handTool = handTool;
  this.globalConnect = globalConnect;
  this.translate = translate;

  // 指定这是一个palette
  palette.registerProvider(this);
}

PaletteProvider.$inject = [
  'palette',
  'create',
  'elementFactory',
  'spaceTool',
  'lassoTool',
  'handTool',
  'globalConnect',
  'translate',
];

PaletteProvider.prototype.getPaletteEntries = function (element) {
  const actions = {};
  const { create, elementFactory, spaceTool, lassoTool, handTool, globalConnect, translate } = this;

  function createAction(type, group, className, title, options) {
    function createListener(event) {
      const shape = elementFactory.createShape(assign({ type }, options));

      if (options) {
        shape.businessObject.di.isExpanded = options.isExpanded;
      }

      create.start(event, shape);
    }

    const shortType = type.replace(/^bpmn:/, '');

    return {
      group,
      className,
      title: title || translate('Create {type}', { type: shortType }),
      action: {
        dragstart: createListener,
        click: createListener,
      },
    };
  }

  function createSubprocess(event) {
    const subProcess = elementFactory.createShape({
      type: 'bpmn:SubProcess',
      x: 0,
      y: 0,
      isExpanded: true,
    });

    const startEvent = elementFactory.createShape({
      type: 'bpmn:StartEvent',
      x: 40,
      y: 82,
      parent: subProcess,
    });

    create.start(event, [subProcess, startEvent], {
      hints: {
        autoSelect: [startEvent],
      },
    });
  }

  function createParticipant(event) {
    create.start(event, elementFactory.createParticipantShape());
  }

  assign(actions, {
    'hand-tool': {
      group: 'tools',
      className: 'bpmn-icon-hand-tool',
      title: translate('Activate the hand tool'),
      action: {
        click: (event) => {
          handTool.activateHand(event);
        },
      },
    },
    'lasso-tool': {
      group: 'tools',
      className: 'bpmn-icon-lasso-tool',
      title: translate('Activate the lasso tool'),
      action: {
        click: (event) => {
          lassoTool.activateSelection(event);
        },
      },
    },
    'space-tool': {
      group: 'tools',
      className: 'bpmn-icon-space-tool',
      title: translate('Activate the create/remove space tool'),
      action: {
        click: (event) => {
          spaceTool.activateSelection(event);
        },
      },
    },
    'global-connect-tool': {
      group: 'tools',
      className: 'bpmn-icon-connection-multi',
      title: translate('Activate the global connect tool'),
      action: {
        click: (event) => {
          globalConnect.start(event);
        },
      },
    },
    'tool-separator': {
      group: 'tools',
      separator: true,
    },
    // 'create.start-event': createAction(
    //   'bpmn:StartEvent',
    //   'event',
    //   'bpmn-icon-start-event-none',
    //   translate('Create StartEvent'),
    // ),
    'create.start-event': createAction(
      'bpmn:StartEvent',
      'event',
      'bpmn-icon-start-event-none',
      translate('Create StartEvent'),
    ),
    'create.intermediate-event-throw-signal': createAction(
      'bpmn:IntermediateThrowEvent',
      'event',
      'bpmn-icon-intermediate-event-throw-signal',
      translate('Create StartEvent'),
      { eventDefinitionType: 'bpmn:SignalEventDefinition' },
    ),
    'create.intermediate-event-catch-signal': createAction(
      'bpmn:IntermediateCatchEvent',
      'event',
      'bpmn-icon-intermediate-event-catch-signal',
      translate('Create Intermediate/Boundary Event'),
      { eventDefinitionType: 'bpmn:SignalEventDefinition' },
    ),
    'create.end-event': createAction(
      'bpmn:EndEvent',
      'event',
      'bpmn-icon-end-event-none',
      translate('Create EndEvent'),
    ),
    'create.exclusive-gateway': createAction(
      'bpmn:ExclusiveGateway',
      'gateway',
      'bpmn-icon-gateway-xor',
      translate('Exclusive Gateway'),
    ),
    'create.parallel-gateway': createAction(
      'bpmn:ParallelGateway',
      'gateway',
      'bpmn-icon-gateway-parallel',
      translate('Parallel Gateway'),
    ),
    'create.inclusive-gateway': createAction(
      'bpmn:InclusiveGateway',
      'gateway',
      'bpmn-icon-gateway-or',
      translate('Inclusive Gateway'),
    ),
    'create.send-task': createAction(
      'bpmn:SendTask',
      'activity',
      'bpmn-icon-send',
      translate('Send Task'),
    ),
    'create.receive-task': createAction(
      'bpmn:ReceiveTask',
      'activity',
      'bpmn-icon-receive',
      translate('Receive Task'),
    ),
    'create.service-task': createAction(
      'bpmn:ServiceTask',
      'activity',
      'bpmn-icon-service',
      translate('Service Task'),
    ),
    // 'create.task': createAction(
    //   'bpmn:Task',
    //   'activity',
    //   'bpmn-icon-task',
    //   translate('Create Task'),
    // ),
    // 'create.data-object': createAction(
    //   'bpmn:DataObjectReference',
    //   'data-object',
    //   'bpmn-icon-data-object',
    //   translate('Create DataObjectReference'),
    // ),
    // 'create.data-store': createAction(
    //   'bpmn:DataStoreReference',
    //   'data-store',
    //   'bpmn-icon-data-store',
    //   translate('Create DataStoreReference'),
    // ),
    // 'create.subprocess-expanded': {
    //   group: 'activity',
    //   className: 'bpmn-icon-subprocess-expanded',
    //   title: translate('Create expanded SubProcess'),
    //   action: {
    //     dragstart: createSubprocess,
    //     click: createSubprocess,
    //   },
    // },
    // 'create.participant-expanded': {
    //   group: 'collaboration',
    //   className: 'bpmn-icon-participant',
    //   title: translate('Create Pool/Participant'),
    //   action: {
    //     dragstart: createParticipant,
    //     click: createParticipant,
    //   },
    // },
    // 'create.group': createAction(
    //   'bpmn:Group',
    //   'artifact',
    //   'bpmn-icon-group',
    //   translate('Create Group'),
    // ),
  });

  return actions;
};
